package com.hospital.dictionary.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.core.utils.StringUtil;
import com.hospital.dictionary.constant.CommonConstant;
import com.hospital.dictionary.module.entity.SysPermission;
import com.hospital.dictionary.module.mapper.SysPermissionMapper;
import com.hospital.dictionary.module.mapper.SysRolePermissionMapper;
import com.hospital.dictionary.module.service.SysPermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Resource
    private SysPermissionMapper sysPermissionMapper;

    @Resource
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Override
    public List<SysPermission> queryByUser(String username) {
        return sysPermissionMapper.queryByUser(username);
    }

    @Override
    public void addPermission(SysPermission sysPermission) {
        if(CommonConstant.MENU_TYPE_0.equals(sysPermission.getMenuType())) {
            sysPermission.setParentId(null);
        }
        //----------------------------------------------------------------------
        String pid = sysPermission.getParentId();
        if(StringUtil.isNotEmpty(pid)) {
            //设置父节点不为叶子节点
            this.sysPermissionMapper.setMenuLeaf(pid, 0);
        }
        sysPermission.setCreateTime(new Date());
        sysPermission.setDelFlag(0);
        sysPermission.setLeaf(true);
        this.save(sysPermission);
    }

    @Override
    public void editPermission(SysPermission sysPermission) {
        SysPermission p = this.getById(sysPermission.getDictId());
        //TODO 该节点判断是否还有子节点
        if(p==null) {
            throw new RuntimeException("未找到菜单信息");
        }else {
            sysPermission.setUpdateTime(new Date());
            //----------------------------------------------------------------------
            //Step1.判断是否是一级菜单，是的话清空父菜单ID
            if(CommonConstant.MENU_TYPE_0.equals(sysPermission.getMenuType())) {
                sysPermission.setParentId("");
            }
            //Step2.判断菜单下级是否有菜单，无则设置为叶子节点
            long count = this.count(new QueryWrapper<SysPermission>().lambda().eq(SysPermission::getParentId, sysPermission.getDictId()));
            if(count==0) {
                sysPermission.setLeaf(true);
            }
            //----------------------------------------------------------------------
            this.updateById(sysPermission);

            //如果当前菜单的父菜单变了，则需要修改新父菜单和老父菜单的，叶子节点状态
            String pid = sysPermission.getParentId();
            if((StringUtil.isNotEmpty(pid) && !pid.equals(p.getParentId())) || StringUtil.isEmpty(pid)&&StringUtil.isNotEmpty(p.getParentId())) {
                //a.设置新的父菜单不为叶子节点
                this.sysPermissionMapper.setMenuLeaf(pid, 0);
                //b.判断老的菜单下是否还有其他子菜单，没有的话则设置为叶子节点
                long cc = this.count(new QueryWrapper<SysPermission>().lambda().eq(SysPermission::getParentId, p.getParentId()));
                if(cc==0) {
                    if(StringUtil.isNotEmpty(p.getParentId())) {
                        this.sysPermissionMapper.setMenuLeaf(p.getParentId(), 1);
                    }
                }

            }
        }
    }

    @Override
    public void deletePermission(String id) {
        SysPermission sysPermission = this.getById(id);
        if(sysPermission==null) {
            throw new RuntimeException("未找到菜单信息");
        }
        String pid = sysPermission.getParentId();
        if(StringUtil.isNotEmpty(pid)) {
            long count = this.count(new QueryWrapper<SysPermission>().lambda().eq(SysPermission::getParentId, pid));
            if(count==1) {
                //若父节点无其他子节点，则该父节点是叶子节点
                this.sysPermissionMapper.setMenuLeaf(pid, 1);
            }
        }
        sysPermissionMapper.deleteById(id);
        // 该节点可能是子节点但也可能是其它节点的父节点,所以需要级联删除
//        this.removeChildrenBy(sysPermission.getId());
        //关联删除
        Map map = new HashMap<>();
        map.put("permission_id",id);
        //删除数据规则
        this.deletePermRuleByPermId(id);
        //删除角色授权表
        sysRolePermissionMapper.deleteByMap(map);
    }

    @Override
    public void deletePermRuleByPermId(String id) {
//        LambdaQueryWrapper<SysPermissionDataRule> query = new LambdaQueryWrapper<>();
//        query.eq(SysPermissionDataRule::getPermissionId, id);
//        int countValue = this.permissionDataRuleService.count(query);
//        if(countValue > 0) {
//            this.permissionDataRuleService.remove(query);
//        }
    }
}
